package org.middleware.enums;
import lombok.Getter;

@Getter
public enum HttpResponseCode {
    // 成功
    SUCCESS(200, "成功"),

    // 客户端错误
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),

    // 服务器错误
    INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),

    // 业务错误 - 6xxxx
    BUSINESS_ERROR(60000, "业务异常"),
    DATA_NOT_EXIST(60001, "数据不存在"),
    DATA_ALREADY_EXISTS(60002, "数据已存在"),
    DATA_INVALID(60003, "数据无效"),

    // 认证授权 - 7xxxx
    LOGIN_FAILED(70001, "登录失败"),
    TOKEN_INVALID(70002, "Token无效"),
    TOKEN_EXPIRED(70003, "Token已过期"),
    PERMISSION_DENIED(70004, "权限不足");

    private final Integer code;
    private final String msg;

    HttpResponseCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 根据code获取枚举
     */
    public static HttpResponseCode getByCode(Integer code) {
        for (HttpResponseCode value : values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }
}
